package pri.jz.keeper.ui.content

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import pri.jz.keeper.ui.theme.DarkOrange
import pri.jz.keeper.ui.view.content.*
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.insets.navigationBarsPadding
import pri.jz.keeper.ui.theme.GhostWhite
import pri.jz.keeper.ui.uistate.ContentViewState
import pri.jz.keeper.ui.view.HomeView

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:
 */

@Composable
fun ContentView(
    viewModel: ContentViewModel = viewModel()
) {
    val viewState by viewModel.viewState.collectAsState()
    when (viewState) {
        ContentViewState.DEFAULT -> DefaultView(viewModel)
        ContentViewState.LOADING -> LoadingView()
        ContentViewState.FAILED -> LoadFailedView()
    }
}

@Composable
private fun DefaultView(
    viewModel: ContentViewModel
) {
    val navController = rememberNavController()
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val destination = navBackStackEntry?.destination?.hierarchy
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        bottomBar = { ContentBottomNavigation(viewModel,navController,destination) },
        floatingActionButton = {
            FloatingScreen(viewModel,navController,destination)
        },
        floatingActionButtonPosition = FabPosition.Center,
        isFloatingActionButtonDocked = true,

        ) { innerPadding ->
        NavigationScreen(viewModel,navController, modifier = Modifier.padding(innerPadding))
    }
}

@Composable
private fun ContentBottomNavigation(
    viewModel: ContentViewModel,
    navController: NavHostController,
    destination: Sequence<NavDestination>?
) {
    val contentScreenItems = listOf( //屏幕导航列表
        ContentScreen.Home,
        ContentScreen.Study,
        ContentScreen.PetButler,
        ContentScreen.News,
        ContentScreen.PersonalCenter
    )
    BottomNavigation(
        elevation = 12.dp,
        backgroundColor = GhostWhite,
        modifier = Modifier.clip(RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)).navigationBarsPadding(true)
    ) {
        contentScreenItems.forEach { screen ->
            val selected = destination?.any{it.route == screen.route} == true
            BottomNavigationItem(
                selected = selected,
                label = { Text(
                    stringResource(screen.labelRes),
                    fontSize = if (screen is ContentScreen.PetButler) 13.sp else TextUnit.Unspecified,
                )},
                icon = {
                    Icon(
                        painterResource(screen.iconRes),
                        contentDescription = null,
                        modifier = Modifier.size(25.dp),
                        tint = if (screen is ContentScreen.PetButler) {
                            Color.White
                        } else if (selected) {
                            DarkOrange
                        } else {
                            Color.Black
                        }
                    )
                },
                onClick = {
                    viewModel.onNavigateClick(navController,screen.route)
                },
                selectedContentColor = DarkOrange,
                unselectedContentColor = Color.Black
            )
        }
    }
}

@Composable
private fun FloatingScreen(
    viewModel: ContentViewModel,
    navController: NavHostController,
    destination: Sequence<NavDestination>?,
) {
    val selected = destination?.any { it.route == ContentScreen.PetButler.route } == true
    val colorAnimation by animateColorAsState(if (selected) DarkOrange else Color.White)
    CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) {
        FloatingActionButton(
            shape = CircleShape,
            onClick = { viewModel.onNavigateClick(navController, ContentScreen.PetButler.route) },
            backgroundColor = colorAnimation,
            elevation = FloatingActionButtonDefaults.elevation(
                defaultElevation = 2.dp,
                pressedElevation = 2.dp,
                focusedElevation = 2.dp,
                hoveredElevation = 0.dp
            ),
        ) {
            Icon(
                painterResource(ContentScreen.PetButler.iconRes),
                contentDescription = null,
                tint = if (selected) Color.White else Color.Unspecified,
                modifier = Modifier.size(40.dp)
            )
        }
    }

}
@Composable
private fun NavigationScreen(
    contentViewModel: ContentViewModel,
    navController: NavHostController,
    modifier: Modifier,
) {
    NavHost(
        navController,
        startDestination = ContentScreen.Home.route,
        modifier = modifier,
    ) {
        composable(ContentScreen.Home.route) {
            HomeView(contentViewModel = contentViewModel)
        }
        composable(ContentScreen.Study.route) {
            StudyView()
        }
        composable(ContentScreen.PetButler.route) {
            PetButlerView()
        }
        composable(ContentScreen.News.route) {
            NewsView()
        }
        composable(ContentScreen.PersonalCenter.route) {
            PersonalCenterView(contentViewModel)
        }
    }
}

@Composable
private fun LoadFailedView(viewModel: ContentViewModel = viewModel()) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .clickable(onClick = viewModel::reLoad),
        contentAlignment = Alignment.Center
    ) {
        Text("加载失败,请点击重试")
    }
}

@Composable
private fun LoadingView() {
    Text("数据加载中。。。")
}

private object NoRippleTheme : RippleTheme {
    @Composable
    override fun defaultColor() = Color.Transparent

    @Composable
    override fun rippleAlpha(): RippleAlpha = RippleAlpha(0.0f,0.0f,0.0f,0.0f)
}

